package rents;

import java.io.Serializable;
import java.util.Set;

import common.money.MonetaryAmount;
import common.money.Percentage;

public class AccountContribution implements Serializable {
	
	private static final long serialVersionUID = -5991988549223471196L;

	private String accountNumber;

	private MonetaryAmount amount;

	private Set<Distribution> distributions;

	
	public AccountContribution(String accountNumber, MonetaryAmount amount, Set<Distribution> distributions) {
		this.accountNumber = accountNumber;
		this.amount = amount;
		this.distributions = distributions;
	}

	
	public String getAccountNumber() {
		return accountNumber;
	}

	
	public MonetaryAmount getAmount() {
		return amount;
	}

	
	public Set<Distribution> getDistributions() {
		return distributions;
	}

	
	public Distribution getDistribution(String beneficiary) {
		for (Distribution d : distributions) {
			if (d.beneficiary.equals(beneficiary)) {
				return d;
			}
		}
		throw new IllegalArgumentException("No such distribution for '" + beneficiary + "'");
	}

	
	public static class Distribution implements Serializable {

		private static final long serialVersionUID = 3896727888758939979L;

		private String beneficiary;

		private MonetaryAmount amount;

		private Percentage percentage;

		private MonetaryAmount totalSavings;

		
		public Distribution(String beneficiary, MonetaryAmount amount, Percentage percentage,
				MonetaryAmount totalSavings) {
			this.beneficiary = beneficiary;
			this.percentage = percentage;
			this.amount = amount;
			this.totalSavings = totalSavings;
		}

		
		public String getBeneficiary() {
			return beneficiary;
		}

		
		public MonetaryAmount getAmount() {
			return amount;
		}

		
		public Percentage getPercentage() {
			return percentage;
		}

		
		public MonetaryAmount getTotalSavings() {
			return totalSavings;
		}

		public String toString() {
			return amount + " to '" + beneficiary + "' (" + percentage + ")";
		}
	}

	public String toString() {
		return "Contribution of " + amount + " to account '" + accountNumber + "' distributed " + distributions;
	}
	
	

}
